package model
{
	import mx.collections.ArrayCollection;
	
	import org.flashIso.engine.core.ILibrary;
	import org.flashIso.engine.library.objects.LBase;
	import org.flashIso.engine.library.objects.LBinaryData;
	import org.flashIso.engine.library.objects.LImage;
	import org.flashIso.engine.library.objects.LObject;
	import org.flashIso.engine.library.objects.LTile;
	import org.robotlegs.mvcs.Actor;
	
	public class ModelLibrary extends Actor implements ILibrary{
		
		public static const defaultTileName:String = "Untitled Tile";
		public static const defaultObjectName:String = "Untitled Object";
		
		[Bindable]
		public var imagesDataProvider : ArrayCollection = new ArrayCollection();
		
		[Bindable]
		public var binaryDataProvider : ArrayCollection = new ArrayCollection();
		
		[Bindable]
		public var objectsDataProvider : ArrayCollection = new ArrayCollection();
		
		[Bindable]
		public var tilesDataProvider : ArrayCollection = new ArrayCollection();
		
		public function clear() : void {
			imagesDataProvider.removeAll();
			binaryDataProvider.removeAll();
			objectsDataProvider.removeAll();
			tilesDataProvider.removeAll();
		}
		public function addLImage(image:LImage) : void {
			if (getLObjectByID(image.id) != null){
				var i:uint;
				while (getLObjectByID(image.id+" "+i) != null){
					i++;
				}
				image.id = image.id+" "+i;
			}
			imagesDataProvider.addItemAt(image, 0);
		}
		public function removeLImage(image:LImage ) : Boolean {
			imagesDataProvider.removeItemAt(imagesDataProvider.getItemIndex(image));
			return true;
		}
		public function getLImageByID(id:String) : LImage {
			var i :uint;
			for (i = 0; i < imagesDataProvider.length; i++){
				if ((imagesDataProvider[i] as LImage).id == id){
					return imagesDataProvider[i];
				}
			}
			return null;
		}
		
		public function addLBinaryData(data:LBinaryData) : void {
			if (getLObjectByID(data.id) != null){
				var i:uint;
				while (getLObjectByID(data.id+" "+i) != null){
					i++;
				}
				data.id = data.id+" "+i;
			}
			binaryDataProvider.addItemAt(data, 0);
		}
		public function removeLBinaryData(data:LBinaryData ) : Boolean {
			binaryDataProvider.removeItemAt(binaryDataProvider.getItemIndex(data));
			return true;
		}
		public function getLBinaryDataByID(id:String) : LBinaryData {
			var i :uint;
			for (i = 0; i < binaryDataProvider.length; i++){
				if ((binaryDataProvider[i] as LBinaryData).id == id){
					return binaryDataProvider[i];
				}
			}
			return null;
		}
		
		public function addLObject(object:LObject) : void {
			if (getLObjectByID(object.id) != null){
				var i:uint;
				while (getLObjectByID(object.id+" "+i) != null){
					i++;
				}
				object.id = object.id+" "+i;
			}
			objectsDataProvider.addItemAt(object, 0);
		}
		public function removeLObject(object:LObject) : Boolean {
			objectsDataProvider.removeItemAt(objectsDataProvider.getItemIndex(object));
			return true;
		}
		public function getLObjectByID(id:String) : LObject {
			var i :uint;
			for (i = 0; i < objectsDataProvider.length; i++){
				if ((objectsDataProvider[i] as LObject).id == id){
					return objectsDataProvider[i];
				}
			}
			return null;
		}
		
		public function addLTile(tile:LTile) : void {
			if (getLTileByID(tile.id) != null){
				var i:uint;
				while (getLTileByID(tile.id+" "+i) != null){
					i++;
				}
				tile.id = tile.id+" "+i;
			}
			tilesDataProvider.addItemAt(tile, 0);
		}
		public function removeLTile(tile:LTile) : Boolean {
			tilesDataProvider.removeItemAt(tilesDataProvider.getItemIndex(tile));
			return true;
		}
		public function getLTileByID(id:String) : LTile {
			var i :uint;
			for (i = 0; i < tilesDataProvider.length; i++){
				if ((tilesDataProvider[i] as LTile).id == id){
					return tilesDataProvider[i];
				}
			}
			return null;
		}
		
		public function toXML() : XML {
			var xml:XML = <Library />;
			
			var xmlImages:XML = <Images />
			var xmlBinaries:XML = <Binaries />
			var xmlObjects:XML = <Objects />
			var xmlTiles:XML = <Tiles />
			
			xml.appendChild(xmlImages);
			xml.appendChild(xmlBinaries);
			xml.appendChild(xmlObjects);
			xml.appendChild(xmlTiles);
			
			var i:uint;
			
			for (i = 0; i < imagesDataProvider.length; i++ ) {
				xmlImages.appendChild((imagesDataProvider[i] as LBase).toXML());
			}
			for (i = 0; i < binaryDataProvider.length; i++ ) {
				xmlBinaries.appendChild((binaryDataProvider[i] as LBase).toXML());
			}
			for (i = 0; i < objectsDataProvider.length; i++ ) {
				xmlObjects.appendChild((objectsDataProvider[i] as LBase).toXML());
			}
			for (i = 0; i < tilesDataProvider.length; i++ ) {
				xmlTiles.appendChild((tilesDataProvider[i] as LBase).toXML());
			}
			
			return xml;
		}
	}
}